home *** CD-ROM | disk | FTP | other *** search
- Path: news.iastate.edu!news
- From: Marybeth Gurski <gurski@cs.iastate.edu>
- Newsgroups: comp.lang.c++
- Subject: Re: Template constructor problem
- Date: Tue, 02 Apr 1996 08:48:18 -0600
- Organization: Dept. Of Computer Science, Iowa State University
- Message-ID: <31613E32.1D07@cs.iastate.edu>
- References: <4jjtgf$ebo@csugrad.cs.vt.edu>
- NNTP-Posting-Host: sunfire.cs.iastate.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.07 9000/712)
-
- Raj Suri wrote:
- >
- > Ok, take this code using g++ v2.7
- >
- > queue.h:
- >
- > template <class QueueItem> class Queue {
- > private:
- > QueueItem buffer[100];
- > int head, tail, count;
- >
- > public:
- > Queue();
- > void Insert (QueueItem item);
- > QueueItem Remove();
- > int Size();
- > ~Queue();
- > };
- >
- > template <class QueueItem>
- > Queue<QueueItem>::Queue() : count(0), head(0), tail(0) {}
- >
- > .....[the rest of the mothods]
- >
- > main.cc:
- >
- > #include<stdio.h>
- > #include "queue.h"
- >
- > Queue<int> myqueue;
- >
- > main () {
- >
- > }
- >
- > Compiler error when linking:
- > collect2: ld returned 1 exit status
- > /usr/lib/cmplrs/cc/ld:
- > Undefined:
- > Queue<int>::Queue(void)
- >
- > If I don't declare a queue object, then I don't get the linker error.
- > Any suggestion?? Thanks in advance.
-
- I've had similar problems with this version of g++. It does't
- support templates the way that you want it to. From the FAQ for g++:
-
-
- -----
- * Support for automatic template instantiation has *not* been enabled
- in the official distribution, due to a disagreement over design
- philosophies. But you can get a patch from Cygnus to turn it on;
- retrieve the patch from
- `ftp://ftp.cygnus.com/pub/g++/gcc-2.7.0-repo.gz'.
- ------
- and:
- ------
- How do I use the new repository code?
- =====================================
-
- Because there is some disagreement about the details of the template
- repository mechanism, you'll need to obtain a patch from Cygnus Support
- to enable the 2.7.0 repository code. You can obtain the patch by
- anonymous FTP: `ftp://ftp.cygnus.com/pub/g++/gcc-2.7.0-repo.gz'.
-
- After you've applied the patch, the `-frepo' flag will enable the
- repository mechanism. The flag works much like the existing
- `-fno-implicit-templates' flag, except that auxiliary files, with an
- `.rpo' extension, are built that specify what template expansions are
- needed. At link time, the (patched) collect program detects missing
- templates and recompiles some of the object files so that the required
- templates are expanded.
-
- Note that the mechanism differs from that of cfront in that template
- definitions still must be visible at the point where they are to be
- expanded. No assumption is made that `foo.C' contains template
- definitions corresponding to template declarations in `foo.h'.
-
- Jason Merrill writes: "To perform closure on a set of objects, just
- try to link them together. It will fail, but as a side effect all
- needed instances will be generated in the objects."
-
- -------
- Problems with the template implementation
- =========================================
-
- g++ does not implement a separate pass to instantiate template
- functions and classes at this point; for this reason, it will not work,
- for the most part, to declare your template functions in one file and
- define them in another. The compiler will need to see the entire
- definition of the function, and will generate a static copy of the
- function in each file in which it is used.
-
- (The experimental template repository code (see *Note repository::)
- that can be added to 2.7.0 does implement a separate pass, but there is
- still no searching of files that the compiler never saw).
-
- For version 2.6.0, however, a new switch `-fno-implicit-templates'
- was added; with this switch, templates are expanded only under user
- control. I recommend that all g++ users that use templates read the
- section "Template Instantiation" in the gcc manual (version 2.6.x and
- newer). g++ now supports explicit template expansion using the syntax
- from the latest C++ working paper:
-
- template class A<int>;
- template ostream& operator << (ostream&, const A<int>&);
- ---------------
-
- So what you might have to do is put
- template class Queue<int>;
- in your Queue.C file.
-
-
- There is lots of more information in the FAQ for g++.
-
-
- -- Marybeth
-
-
- --
- --------------------------------------------------------------
- Marybeth Gurski
- gurski@cs.iastate.edu
- http://www.cs.iastate.edu/~gurski/homepage.html
-